<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherPHP
 * @subpackage   Views
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class View
{
	public $controller;
	public $here;
	
	public $params;
	public $data;
	public $load;
	
	public    $viewVars  = array();
#	protected $viewPath  = '';
	public    $viewPath  = '';
	public    $templates = ''; 
	
	function __construct(Controller $controller=null)
	{
		// loader
		$this->load = Loader::instance('Loader');
		$this->load->view = $this;
		
		// controller
		if ($controller)
		{
			$this->controller =  $controller;
			$this->here       =  $controller->here;
			$this->params     =& $controller->params;
			$this->viewPath   =  $controller->viewPath;
			$this->templates  =  $controller->_templates;
		}
	}
	
	function render($action, $layout, &$viewVars)
	{
		// vars
		$this->viewVars =& $viewVars;
		
		//helpers
		$this->load->helpers();
		
		if (isset($this->controller))
		{
			// components
			foreach(Config::$autoloads['components'] as $class) {
				$this->viewVars[$class] = $this->controller->$class;
			}
		}
		
		if (!empty($action))
			$action = $this->viewPath.DS.$action;
		
		// view + layout
		echo $this->__render($action, $layout);
	}
	
	function renderElement($element, $viewVars=null)
	{
		return $this->__render('elements'.DS.$element, null, $viewVars, false);
	}
	
	function elementExists($element)
	{
		return (file_exists(APP.'views'.DS.'elements'.DS."$element.thtml")
			or file_exists(FEATHER.'views'.DS.'elements'.DS."$element.thtml"));
	}
	
	// internals
	
	protected function __render($file, $layout=null, $viewVars=null, $template=true)
	{
		// vars
		foreach(array_keys($this->viewVars) as $var)
			$$var =& $this->viewVars[$var];
		
		if (!empty($viewVars))
		{
			foreach(array_keys($viewVars) as $var) {
				$$var =& $viewVars[$var];
			}
		}
		
		if (isset($this->controller))
		{
			$data =& $this->controller->data;
			$title_for_layout = $this->controller->pageTitle;
		}
		
		// view or element
		if (!empty($file))
		{
			ob_start();
			
			if (file_exists(APP.'views'.DS."$file.thtml")) {
				require APP.'views'.DS."$file.thtml";
			}
			elseif ($template)
			{
				if (!empty($this->templates))
					$file = preg_replace(':^([^'.DS.']+'.DS.'):', $this->templates.DS, $file);
				
				if (file_exists(FEATHER.'views'.DS.'templates'.DS."$file.thtml")) {
					require FEATHER.'views'.DS.'templates'.DS."$file.thtml";
				}
				elseif (DEBUG) {
					trigger_error(sprintf("Unable to find view '%s'.", $file), E_USER_ERROR);
				}
				else {
					Error::show(404);
				}
			}
			else
			{
				if (file_exists(FEATHER.'views'.DS."$file.thtml")) {
					require FEATHER.'views'.DS."$file.thtml";
				}
				elseif (DEBUG) {
					trigger_error(sprintf("Unable to find view '%s'.", $file), E_USER_ERROR);
				}
				else {
					Error::show(404);
				}
			}
			$content_for_layout = ob_get_clean();
		}
		
		// layout?
		if (!is_null($layout))
		{
			ob_start();
			
			if (file_exists(APP.'views'.DS.'layouts'.DS."$layout.thtml"))
				require APP.'views'.DS.'layouts'.DS."$layout.thtml";
			elseif (file_exists(FEATHER.'views'.DS.'templates'.DS.'layouts'.DS."$layout.thtml"))
				require FEATHER.'views'.DS.'templates'.DS.'layouts'.DS."$layout.thtml";
			elseif (DEBUG)
				trigger_error(sprintf("Unable to find layout '%s'.", $layout), E_USER_ERROR);
			else
				Error::show(404);
			return ob_get_clean();
		}
		
		return $content_for_layout;
	}
	
}
?>